package com.ruoyi.project.wxapi.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.meal.domain.MealServiceObjects;
import com.ruoyi.project.meal.domain.MealServiceObjectsList;
import com.ruoyi.project.meal.service.IMealServiceObjectsListService;
import com.ruoyi.project.meal.service.IMealServiceObjectsService;
import com.ruoyi.project.meal.utils.DataParse;
import com.ruoyi.project.wxapi.model.dto.MealDTO;
import com.ruoyi.project.wxapi.model.dto.Result;
import com.ruoyi.project.wxapi.model.qo.BaseParamQO;
import io.swagger.annotations.Api;
import me.chanjar.weixin.common.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("报餐程序")
@RestController
@RequestMapping("/wxapi/meal")
public class WxapiMealController {
    @Autowired
    IMealServiceObjectsService mealServiceObjectsService;
    @Autowired
    IMealServiceObjectsListService mealListService;

    @RequestMapping("/lists")
    public Result lists(String className,String reportDate,String eat,String person,String food) {
        MealServiceObjectsList mealServiceObjectsList=new MealServiceObjectsList();
        mealServiceObjectsList.setClassName(className);
        mealServiceObjectsList.setEat(eat);
        mealServiceObjectsList.setPerson(person);
        mealServiceObjectsList.setFood(food);
        if(!StringUtils.isEmpty(reportDate)){
            mealServiceObjectsList.setReportDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,reportDate));
        }
        return Result.success(mealListService.selectMealServiceObjectsListList(mealServiceObjectsList));
    }

    @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
    @PostMapping("/insert")
    public Result insert(@RequestBody List<MealServiceObjectsList> list) {
        if(list==null || list.size()==0){
            Result.error("数据不能为空");
        }
        for (MealServiceObjectsList d:list) {
            mealListService.insertMealServiceObjectsList(d);
        }
//组装数据
        MealServiceObjects mealServiceObjects =new MealServiceObjects();
        if (list!=null && list.size()>0 ) {
            mealServiceObjects.setCampus(list.get(0).getCampus());
            mealServiceObjects.setReportDate(list.get(0).getReportDate());
            mealServiceObjects.setClassName(list.get(0).getClassName());
            List<MealServiceObjects> mL=mealServiceObjectsService.selectMealServiceObjectsList(mealServiceObjects);
            //判断是新增还是更新
            List<MealServiceObjects> sList= DataParse.parseObjects(list);
            for (MealServiceObjects  so:sList   ) {
                if(mL==null || mL.size()==0){
                    //新增
                    mealServiceObjectsService.insertMealServiceObjects(so);
                } else {
                    //更新
                    for (MealServiceObjects soT:mL ) {
                        if(so.getFoodType().equals(soT.getFoodType())){
                            so.setId(soT.getId());
                            mealServiceObjectsService.updateMealServiceObjects(so);
                        }
                    }
                }
            }
            }
        return Result.success("新增成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody List<MealServiceObjectsList> list) {
        if(list==null || list.size()==0){
            Result.error("数据不能为空");
        }
        for (MealServiceObjectsList d:list) {
            mealListService.updateMealServiceObjectsList(d);
        }
        //组装数据
        MealServiceObjects mealServiceObjects =new MealServiceObjects();
        if (list!=null && list.size()>0 ) {
            mealServiceObjects.setCampus(list.get(0).getCampus());
            mealServiceObjects.setReportDate(list.get(0).getReportDate());
            mealServiceObjects.setClassName(list.get(0).getClassName());
            List<MealServiceObjects> mL=mealServiceObjectsService.selectMealServiceObjectsList(mealServiceObjects);
            //判断是新增还是更新
            List<MealServiceObjects> sList= DataParse.parseObjects(list);
            for (MealServiceObjects  so:sList   ) {
                if(mL==null || mL.size()==0){
                    //新增
                    mealServiceObjectsService.insertMealServiceObjects(so);
                } else {
                    //更新
                    for (MealServiceObjects soT:mL ) {
                        if(so.getFoodType().equals(soT.getFoodType())){
                            so.setId(soT.getId());
                            mealServiceObjectsService.updateMealServiceObjects(so);
                        }
                    }
                }
            }
        }
        return Result.success("更新成功");
    }




    public MealServiceObjects listToObje(List<MealServiceObjectsList> list){
        MealServiceObjects mealServiceObjects=new MealServiceObjects();
        mealServiceObjects.setClassName(list.get(0).getClassName());
        mealServiceObjects.setReportDate(list.get(0).getReportDate());
        List<MealServiceObjects> objectList=mealServiceObjectsService.selectReportList(mealServiceObjects);
        if(objectList!=null && objectList.size()>0){
            mealServiceObjects=objectList.get(0);
        }
        mealServiceObjects=new MealServiceObjects();
        for (MealServiceObjectsList d:list) {
            mealServiceObjects.setId(d.getServiceObjectsId());
            mealServiceObjects.setClassName(d.getClassName());
            mealServiceObjects.setReportDate(d.getReportDate());
            mealServiceObjects.setReportingPerson(d.getPerson());

            switch (d.getPerson()){
                case "特困":
                    switch (d.getEat()){
                        case "早餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmPoor() ==null) mealServiceObjects.setmPoor(0);
                                mealServiceObjects.setmPoor(mealServiceObjects.getmPoor()+d.getNumber().intValue());
                            }
                            break;
                        case "午餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getlPoor() ==null) mealServiceObjects.setlPoor(0);
                                mealServiceObjects.setlPoor(mealServiceObjects.getlPoor()+d.getNumber().intValue());
                            }
                            break;
                        case "晚餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getdPoor() ==null) mealServiceObjects.setdPoor(0);
                                mealServiceObjects.setdPoor(mealServiceObjects.getdPoor()+d.getNumber().intValue());
                            }
                            break;
                    }
                    break;
                case "政府供养":
                    switch (d.getEat()){
                        case "早餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmGove() ==null) mealServiceObjects.setmGove(0);
                                mealServiceObjects.setmGove(mealServiceObjects.getmGove()+d.getNumber().intValue());
                            }
                            break;
                        case "午餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getlGove() ==null) mealServiceObjects.setlGove(0);
                                mealServiceObjects.setlGove(mealServiceObjects.getlGove()+d.getNumber().intValue());
                            }
                            break;
                        case "晚餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmGove() ==null) mealServiceObjects.setmGove(0);
                                mealServiceObjects.setdGove(mealServiceObjects.getmGove()+d.getNumber().intValue());
                            }
                            break;
                    }
                    break;
                case "事无":
                    switch (d.getEat()){
                        case "早餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmNothing() ==null) mealServiceObjects.setmNothing(0);
                                mealServiceObjects.setmNothing(mealServiceObjects.getmNothing()+d.getNumber().intValue());
                            }
                            break;
                        case "午餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getlNothing() ==null) mealServiceObjects.setlNothing(0);
                                mealServiceObjects.setlNothing(mealServiceObjects.getlNothing()+d.getNumber().intValue());
                            }
                            break;
                        case "晚餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getdNothing() ==null) mealServiceObjects.setdNothing(0);
                                mealServiceObjects.setdNothing(mealServiceObjects.getmNothing()+d.getNumber().intValue());
                            }
                            break;
                    }
                    break;
                case "孤儿":
                    switch (d.getEat()){
                        case "早餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmOrphan() ==null) mealServiceObjects.setmOrphan(0);
                                mealServiceObjects.setmOrphan(mealServiceObjects.getmOrphan()+d.getNumber().intValue());
                            }
                            break;
                        case "午餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getlOrphan() ==null) mealServiceObjects.setlOrphan(0);
                                mealServiceObjects.setlOrphan(mealServiceObjects.getlOrphan()+d.getNumber().intValue());
                            }
                            break;
                        case "晚餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getdOrphan() ==null) mealServiceObjects.setdOrphan(0);
                                mealServiceObjects.setdOrphan(mealServiceObjects.getdOrphan()+d.getNumber().intValue());
                            }
                            break;
                    }
                    break;
                case "自费":
                    switch (d.getEat()){
                        case "早餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getmExpense() ==null) mealServiceObjects.setmExpense(0);
                                mealServiceObjects.setmExpense(mealServiceObjects.getmExpense()+d.getNumber().intValue());
                            }
                            break;
                        case "午餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getlExpense() ==null) mealServiceObjects.setlExpense(0);
                                mealServiceObjects.setlExpense(mealServiceObjects.getlExpense()+d.getNumber().intValue());
                            }
                            break;
                        case "晚餐":
                            if(isTotal(d.getFood())){
                                if(mealServiceObjects.getdExpense() ==null) mealServiceObjects.setdExpense(0);
                                mealServiceObjects.setdExpense(mealServiceObjects.getdExpense()+d.getNumber().intValue());
                            }
                            break;
                    }
                    break;
            }
        }
        return  mealServiceObjects;
    }

    /**
     * 是否要统计数量
     * @param food
     * @return
     */
    public boolean isTotal(String food){
        if("普食".equals(food)|| "流食".equals(food)){
            return  true;
        }
        return false;
    }
}
